﻿/*
 * ================================================================================
 *          File: frmShopProfits.cs
 *          Desc:利润查看
 *          Auth:JF
 *          Date:2011-10-18
 *================================================================================
 *          Change History
 *================================================================================
 *          Date            Author          Description
 *          --------        --------        ----------------
 *          
 * 
 *================================================================================
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using EShop.BLL;

namespace EShop.Client
{
    public partial class frmShopProfits : frmBase
    {
        public frmShopProfits()
        {
            InitializeComponent();
        }

        TreeNode tnMain;
        TreeNode tnYear;
        TreeNode tnMonth;
        TreeNode tnDay;

        DataTable dataSource = new DataTable();
        ShopReportBLL oShopReportBLL = new ShopReportBLL();

        private void frmShopProfits_Load(object sender, EventArgs e)
        {
            tnMain = new TreeNode("[eShop]利润查看");
            tnMain.Nodes.Add("");
            treeItems.Nodes.Add(tnMain);
            GetProfitsByYear();
        }
        private void GetProfitsByYear()
        {
            dataSource = oShopReportBLL.GetProfits(0, null, null, null, null);
            dgvResult.Columns["Items"].FillWeight = 80;
            dgvResult.Columns["RealAmount"].FillWeight = 120;
            dgvResult.Columns["RealAmount"].HeaderText = "总销售额";
            dgvResult.Columns["BuyCost"].HeaderText = "所费成本";
            dgvResult.Columns["Profits"].HeaderText = "利润";
            dgvResult.Columns["Items"].HeaderText = "年份";
            dgvResult.Rows.Clear();
            int index = -1;
            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                index = dgvResult.Rows.Add();
                dgvResult.Rows[index].Cells["Items"].Value = dataSource.Rows[i]["Items"]+"年";
                dgvResult.Rows[index].Cells["RealAmount"].Value = dataSource.Rows[i]["RealAmount"];
                dgvResult.Rows[index].Cells["BuyCost"].Value = dataSource.Rows[i]["BuyCost"];
                dgvResult.Rows[index].Cells["Profits"].Value = dataSource.Rows[i]["Profits"];
            }
        }
        private void GerProfitsByMonth(string strYear)
        {
            dataSource = oShopReportBLL.GetProfits(1, strYear, null, null, null);
            dgvResult.Columns["Items"].HeaderText = "月份(" + strYear + "年度)";
            dgvResult.Columns["Items"].FillWeight = 120;
            dgvResult.Columns["RealAmount"].FillWeight = 120;
            dgvResult.Columns["RealAmount"].HeaderText = "总销售额";
            dgvResult.Columns["BuyCost"].HeaderText = "所费成本";
            dgvResult.Columns["Profits"].HeaderText = "利润";
            dgvResult.Rows.Clear();
            int index = -1;
            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                index = dgvResult.Rows.Add();
                dgvResult.Rows[index].Cells["Items"].Value = dataSource.Rows[i]["Items"]+"月";
                dgvResult.Rows[index].Cells["RealAmount"].Value = dataSource.Rows[i]["RealAmount"];
                dgvResult.Rows[index].Cells["BuyCost"].Value = dataSource.Rows[i]["BuyCost"];
                dgvResult.Rows[index].Cells["Profits"].Value = dataSource.Rows[i]["Profits"];
            }
        }
        private void GetProfitsByDay(string strYear, string strMonth)
        {
            dataSource = oShopReportBLL.GetProfits(2, strYear, strMonth, null, null);
            dgvResult.Columns["Items"].HeaderText = "日期(" + strYear + "年" + strMonth + "月)";
            dgvResult.Columns["Items"].FillWeight = 80;
            dgvResult.Columns["RealAmount"].FillWeight = 120;
            dgvResult.Columns["RealAmount"].HeaderText = "总销售额";
            dgvResult.Columns["BuyCost"].HeaderText = "所费成本";
            dgvResult.Columns["Profits"].HeaderText = "利润";
            dgvResult.Rows.Clear();
            int index = -1;
            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                index = dgvResult.Rows.Add();
                dgvResult.Rows[index].Cells["Items"].Value = dataSource.Rows[i]["Items"]+"日";
                dgvResult.Rows[index].Cells["RealAmount"].Value = dataSource.Rows[i]["RealAmount"];
                dgvResult.Rows[index].Cells["BuyCost"].Value = dataSource.Rows[i]["BuyCost"];
                dgvResult.Rows[index].Cells["Profits"].Value = dataSource.Rows[i]["Profits"];
            }
        }
        private void GetProfitsByOrderID(string strYear, string strMonth, string strDay)
        {
            dataSource = oShopReportBLL.GetProfits(3, strYear, strMonth, strDay, null);
            dgvResult.Columns["Items"].HeaderText = "销售单号(" + strYear + "年" + strMonth + "月" + strDay + "日)";
            dgvResult.Columns["Items"].FillWeight = 200;
            dgvResult.Columns["RealAmount"].FillWeight = 120;
            dgvResult.Columns["RealAmount"].HeaderText = "总销售额";
            dgvResult.Columns["BuyCost"].HeaderText = "所费成本";
            dgvResult.Columns["Profits"].HeaderText = "利润";
            dgvResult.Rows.Clear();
            int index = -1;
            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                index = dgvResult.Rows.Add();
                dgvResult.Rows[index].Cells["Items"].Value = "单号："+dataSource.Rows[i]["Items"];
                dgvResult.Rows[index].Cells["RealAmount"].Value = dataSource.Rows[i]["RealAmount"];
                dgvResult.Rows[index].Cells["BuyCost"].Value = dataSource.Rows[i]["BuyCost"];
                dgvResult.Rows[index].Cells["Profits"].Value = dataSource.Rows[i]["Profits"];
            }
        }
        private void GetOrderID_Detail(string strYear, string strMonth, string strDay, string strOrderID)
        {
            dataSource = oShopReportBLL.GetProfits(4, strYear, strMonth, strDay, strOrderID);
            dgvResult.Columns["Items"].HeaderText = "商品条码 (单号：" + strOrderID +")";
            dgvResult.Columns["Items"].FillWeight = 150;
            dgvResult.Columns["RealAmount"].FillWeight = 200;
            dgvResult.Columns["RealAmount"].HeaderText = "商品名称";
            dgvResult.Columns["BuyCost"].HeaderText = "商品单位";
            dgvResult.Columns["Profits"].HeaderText = "销售数量";
            dgvResult.Rows.Clear();
            int index = -1;
            for (int i = 0; i < dataSource.Rows.Count; i++)
            {
                index = dgvResult.Rows.Add();
                dgvResult.Rows[index].Cells["Items"].Value = dataSource.Rows[i]["Barcode"];
                dgvResult.Rows[index].Cells["RealAmount"].Value = dataSource.Rows[i]["MerchandiseName"];
                dgvResult.Rows[index].Cells["BuyCost"].Value = dataSource.Rows[i]["UnitName"];
                dgvResult.Rows[index].Cells["Profits"].Value = dataSource.Rows[i]["BuyNumber"];
            }
        }

        private void treeItems_BeforeExpand(object sender, TreeViewCancelEventArgs e)
        {
            int index = e.Node.Index;
            int level = e.Node.Level;
            e.Node.Nodes.Clear();
            string strYear, strMonth, strDay;
            switch (level)
            {
                case 0:
                    dataSource = oShopReportBLL.GetProfits(0, null, null, null, null);
                    for (int i = 0; i < dataSource.Rows.Count; i++)
                    {
                        tnYear = new TreeNode("年份："+dataSource.Rows[i]["Items"].ToString());
                        tnYear.Nodes.Add("");
                        e.Node.Nodes.Add(tnYear);
                    }
                    break;
                case 1:
                    strYear = e.Node.Text.Substring(3);
                    dataSource = oShopReportBLL.GetProfits(1, strYear, null, null, null);
                    for (int i = 0; i < dataSource.Rows.Count; i++)
                    {
                        tnMonth = new TreeNode("月份：" + dataSource.Rows[i]["Items"].ToString());
                        tnMonth.Nodes.Add("");
                        e.Node.Nodes.Add(tnMonth);
                    }
                    break;
                case 2:
                    strYear = e.Node.Parent.Text.Substring(3);
                    strMonth = e.Node.Text.Substring(3);
                    dataSource = oShopReportBLL.GetProfits(2, strYear, strMonth, null, null);
                    for (int i = 0; i < dataSource.Rows.Count; i++)
                    {
                        tnDay = new TreeNode("日期："+dataSource.Rows[i]["Items"].ToString());
                        tnDay.Nodes.Add("");
                        e.Node.Nodes.Add(tnDay);
                    }
                    break;
                case 3:
                    strYear = e.Node.Parent.Parent.Text.Substring(3);
                    strMonth = e.Node.Parent.Text.Substring(3);
                    strDay = e.Node.Text.Substring(3);
                    dataSource = oShopReportBLL.GetProfits(3, strYear, strMonth, strDay, null);
                    for (int i = 0; i < dataSource.Rows.Count; i++)
                    {
                        e.Node.Nodes.Add("单号："+dataSource.Rows[i]["Items"].ToString());
                    }
                    break;
                case 4:
                    break;
                default:
                    break;
            }
        }

        private void treeItems_BeforeSelect(object sender, TreeViewCancelEventArgs e)
        {
            string strYear = null, strMonth = null, strDay = null, strOrderID = null;
            int level = e.Node.Level;
            if (level == -1) return;
            TreeNode eNode = treeItems.SelectedNode;
            switch (level)
            {
                case 0:
                    GetProfitsByYear();
                    break;
                case 1:
                    strYear = e.Node.Text.Substring(3);
                    GerProfitsByMonth(strYear);
                    break;
                case 2:
                    strYear = e.Node.Parent.Text.Substring(3);
                    strMonth = e.Node.Text.Substring(3);
                    GetProfitsByDay(strYear, strMonth);
                    break;
                case 3:
                    strYear = e.Node.Parent.Parent.Text.Substring(3);
                    strMonth = e.Node.Parent.Text.Substring(3);
                    strDay = e.Node.Text.Substring(3);
                    GetProfitsByOrderID(strYear, strMonth, strDay);
                    break;
                case 4:
                    strYear = e.Node.Parent.Parent.Parent.Text.Substring(3);
                    strMonth = e.Node.Parent.Parent.Text.Substring(3);
                    strDay = e.Node.Parent.Text.Substring(3);
                    strOrderID = e.Node.Text.Substring(3);
                    GetOrderID_Detail(strYear, strMonth, strDay, strOrderID);
                    break;
                default:
                    GetProfitsByYear();
                    break;
            }
        }
    }
}
